<?php
/*
 * Creative Bulletin Board Generation 3
 * Version 1.0 Beta 1
 +------------------------------------
 * Core Debugger Management Script
 * Started: September 20, 2009 05:00 PM
 * Script version: 1.0
 +------------------------------------
 * Purpose: Run the debugger. Takes out all the work from injecting debug code into layouts (although they DO have to call them.)
 */
class debugger{
	public function run_debugger($dbg_array){
		global $view;
		$queriesArray = explode("\n", $dbg_array['queries_ran']);
		sort($queriesArray);
		$cnt = count($queriesArray);
		$selectedTablesArray = array();
		for($i=1;$i!=$cnt;$i++){
			if($i!=count($queriesArray)-1){ # Isn't Last
				if(strtolower($queriesArray[$i]) === strtolower($queriesArray[$i+1])){
					if(!preg_match("#<img src='public/icon/exclamation.png' alt='(.*)' />#si", $queriesArray[$i])){
						$queriesArray[$i] .= " <img src='public/icon/exclamation.png' alt='{$i}' /> <b>Repeated query. Consider revising.</b>";
					}
					$queriesArray[$i+1] .= " <img src='public/icon/exclamation.png' alt='{$i}+1' /> <b>Repeated query. Consider revising.</b>";
				}
				elseif(substr(trim($queriesArray[$i]), 0, 6) == "SELECT" and !preg_match("/SQL_CACHE/si", $queriesArray[$i])){
					if(!preg_match("#<img src='public/icon/exclamation.png' alt='(.*)' />#si", $queriesArray[$i])){
						$queriesArray[$i] .= " <img src='public/icon/error.png' alt='{$i}' /> <b>Query not using SQL Cache. Consider revising.</b>";
					}
				}
				else{
					if(!preg_match("#<img src='public/icon/exclamation.png' alt='(.*)' />#si", $queriesArray[$i])){
						$queriesArray[$i] .= " <img src='public/icon/accept.png' alt='{$i}' />";
					}
				}
			}
			else{
				if(!preg_match("#<img src='public/icon/exclamation.png' alt='(.*)' />#si", $queriesArray[$i])){
					$queriesArray[$i] .= " <img src='public/icon/accept.png' alt='{$i}' />";
				}
			}
		}
		$queriesString = implode("\n", $queriesArray);
		$dbg_array['queries_ran'] = $queriesString;
		// Dump SGPC
		$dbg_array['dumpS'] = print_r($_SESSION, true);
		$dbg_array['dumpG'] = print_r($_GET, true);
		$dbg_array['dumpP'] = print_r($_POST, true);
		$dbg_array['dumpC'] = print_r($_COOKIE, true);
		return $view->showTemplate("advanced_debug", $dbg_array, 1);
	}
}
